package org.session.dao.impl;

import javax.ejb.Stateless;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.session.dao.InstanceDao;
import org.session.dao.SeriesDao;
import org.entity.Instance;
import org.entity.Series;
import org.entity.Study;

/**
 * Session Bean implementation class InstanceDaoImpl
 */

@SuppressWarnings("unused")
@Stateless
@Local(InstanceDao.class)
@Remote(InstanceDao.class)
public class InstanceDaoImpl implements InstanceDao {

	@PersistenceContext
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public InstanceDaoImpl() {
        // TODO Auto-generated constructor stub
    }

	/* (non-Javadoc)
	 * @see org.session.dao.InstanceDao#findAllInstance()
	 */
	@SuppressWarnings("unchecked")
	public List<Instance> findAllInstance() {
		// TODO Auto-generated method stub
		return (List<Instance>) em.createQuery("select inst from Instance inst").getResultList();
	}

	/* (non-Javadoc)
	 * @see org.session.dao.InstanceDao#findInstanceByPk(java.lang.Integer)
	 */
	public Instance findInstanceByPk(Integer pk) {
		// TODO Auto-generated method stub
		return em.find(Instance.class, pk);
	}

	/* (non-Javadoc)
	 * @see org.session.dao.InstanceDao#findInstanceBySeriesPk(java.lang.Integer)
	 */
	public List<Instance> findInstanceBySeriesPk(Integer pk) {
		Query query = em.createQuery("select inst from Instance inst where inst.series.pk =:pk");
		query.setParameter("pk", pk);
		@SuppressWarnings("unchecked")
		List<Instance> inst = query.getResultList();
		return inst;
	}

	/* (non-Javadoc)
	 * @see org.session.dao.InstanceDao#update(org.entity.Instance)
	 */
	public void update(Instance instance) {
		em.merge(instance);
		
	}

}
